package com.tchepannou.limbe.gae.converter;

import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.KeyFactory;
import com.tchepannou.util.StringUtil;
import com.tchepannou.util.converter.Converter;

import javax.mail.internet.InternetAddress;

public class EmailConverter
        implements Converter
    {
        //-- Converter overrides
        @Override
        public Object convert(Object value)
        {
            if (value instanceof String)
            {
                String email = (String)value;
                return StringUtil.isEmail(email) ? new Email(email) : null;
            }
            else if (value instanceof InternetAddress)
            {
                return new Email(((InternetAddress)value).getAddress());
            }
            else
            {
                return null;
            }
        }

}
